package com.paireach.szwolaiyun.util;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.paireach.szwolaiyun.base64.CryptoUtil;
import com.paireach.szwolaiyun.sz_util.PhoneUtil;
import com.paireach.szwolaiyun.sz_util.SharedPreferencesUtil;

import org.apache.http.entity.StringEntity;
import org.json.JSONObject;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by Administrator on 2016/6/22.
 */
public class MyHttpUtil {
    // APP后台服务器IP地址
//	public static String APP_SERVER_ADDRESS = "http://192.168.1.12:8089";//内部服务器
//    public static String APP_SERVER_ADDRESS = "http://14w92q3315.iask.in";//12外部映射
//	public static String APP_SERVER_ADDRESS = "http://192.168.1.14:8089";//Allen
//	public static String APP_SERVER_ADDRESS = "http://192.168.1.13:8089";//Ocean
//	public static String APP_SERVER_ADDRESS = "http://192.168.1.15:8090";//Kevin
//	public static String APP_SERVER_ADDRESS = "http://139.196.188.185";//aliyun
//	public static String APP_SERVER_ADDRESS = "http://139.196.189.158";//生产服务器
//	public static String APP_SERVER_ADDRESS = "http://wly.paireach.com";//生产服务器

    //测试地址http://192.168.1.27:8085
    public static String APP_SERVER_ADDRESS = "http://192.168.1.27:8085";


    // APP后台服务器项目名称
    public static String APP_SERVER_PROJECT = "/itip";
//	public static String APP_SERVER_PROJECT = "";

    // APP后台服务涉及接口类型
    public static String APP_SERVER_INTFACE_TYPE = "/szitipwebsvc/receiveWoYunServiceRest";

    // APP后台服务涉及接口类型+项目名称+接口类型
    public static String APP_SERVER_INTFACE_START = APP_SERVER_ADDRESS + APP_SERVER_PROJECT + APP_SERVER_INTFACE_TYPE;

    // APP后台服务接口名称
    public static String APP_SERVER_INTFACE_NAME = "/busnessTtsMesssage";

    public static void Login(Context context, String tel, String psw, final Handler handler) {
        HttpUtils http = new HttpUtils();
        RequestParams params = new RequestParams();
        JSONObject aMap = new JSONObject();
        JSONObject obj = new JSONObject();
        try {
            psw = CryptoUtil.digestByMD5(psw);
            aMap.put("tel", tel);
            aMap.put("pwd", psw);
            aMap.put("phoneCard", JPushInterface.getRegistrationID(context));// 登录设备编码
            aMap.put("currAppVersion", CryptoUtil.digestByEncode(getVersion(context)));// 司机当前APP版本
            aMap.put("currAppSysVersion", CryptoUtil.digestByEncode(new PhoneUtil().toString()));// 司机手机信息
            obj.put("requestEntity", aMap);
            obj.put("type", HttpEnum.LOGIN.getCode());
            obj.put("operation", HttpEnum.LOGIN.getName());
            params.setBodyEntity(new StringEntity(obj.toString()));
            params.setContentType("application/json;charset=UTF-8");
            http.configTimeout(30 * 1000); // 配置超时时间：30秒
            String reqUrl = APP_SERVER_INTFACE_START
                    + APP_SERVER_INTFACE_NAME;
            http.send(HttpRequest.HttpMethod.POST, reqUrl, params,
                    new RequestCallBack<String>() {
                        public void onSuccess(ResponseInfo<String> responseInfo) {
                            Message message = new Message();
                            message.what = MessageInfoUtil.LOGIN_SUCCEED;
                            message.obj = responseInfo.result;
                            handler.sendMessage(message);
                        }

                        public void onFailure(HttpException error, String msg) {
                            Message message = new Message();
                            message.what = MessageInfoUtil.LOGIN_FAIL;
                            message.obj = msg;
                            handler.sendMessage(message);
                        }
                    });
        } catch (Exception e) {
            Log.e("0011", "请求异常", e);
        }


    }

    /**
     * 获得所有派单信息
     */
    public static void getAllDispatch(Context context, final Handler handler) {
        HttpUtils http = new HttpUtils();
        // phone-手机、orderCode-运单号
        RequestParams params = new RequestParams();
        JSONObject obj = new JSONObject();
        JSONObject map = new JSONObject();
        try {
            map.put("phone", SharedPreferencesUtil.getValue(context, SharedPreferencesUtil.USER_PHONE));
            obj.put("requestEntity", map);
            obj.put("type", HttpEnum.GET_DRIVER_ORDERLIST_PAGE.getCode());
            obj.put("operation", HttpEnum.GET_DRIVER_ORDERLIST_PAGE.getName());
            params.setBodyEntity(new StringEntity(obj.toString()));
            params.setContentType("application/json;charset=UTF-8");
            http.configTimeout(30 * 1000); // 配置超时时间：30秒
            String reqUrl = APP_SERVER_INTFACE_START
                    + APP_SERVER_INTFACE_NAME;
            http.send(HttpRequest.HttpMethod.POST, reqUrl, params, new RequestCallBack<String>() {

                @Override
                public void onSuccess(ResponseInfo<String> responseInfo) {
                    Log.e("0011", "获取所有派单信息" + responseInfo.result);
                    Message message = new Message();
                    message.what = MessageInfoUtil.GET_DISPATCH_SUCCEED;
                    message.obj = responseInfo.result;
                    handler.sendMessage(message);

                }

                @Override
                public void onFailure(HttpException e, String s) {
                    Log.e("0011", "获取所有派单信息失败" + s, e);
                    Message message = new Message();
                    message.what = MessageInfoUtil.GET_DISPATCH_FAIL;
                    message.obj = s;
                    handler.sendMessage(message);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 用于查询司机当前运单的信息
     */
    public static void getUserDispatch(Context context, final Handler handler) {
        HttpUtils http = new HttpUtils();
        // phone-手机、orderCode-运单号
        RequestParams params = new RequestParams();
        JSONObject obj = new JSONObject();
        JSONObject map = new JSONObject();
        try {
            map.put("phone", SharedPreferencesUtil.getValue(context, SharedPreferencesUtil.USER_PHONE));
            obj.put("requestEntity", map);
            obj.put("type", HttpEnum.GET_DRIVER_ORDERLIST_ONE.getCode());
            obj.put("operation", HttpEnum.GET_DRIVER_ORDERLIST_ONE.getName());
            params.setBodyEntity(new StringEntity(obj.toString()));
            params.setContentType("application/json;charset=UTF-8");
            http.configTimeout(30 * 1000); // 配置超时时间：30秒
            String reqUrl = APP_SERVER_INTFACE_START
                    + APP_SERVER_INTFACE_NAME;
            http.send(HttpRequest.HttpMethod.POST, reqUrl, params, new RequestCallBack<String>() {

                @Override
                public void onSuccess(ResponseInfo<String> responseInfo) {
                    Log.e("0011", "获取所有派单信息" + responseInfo.result);
                    Message message = new Message();
                    message.what = MessageInfoUtil.GET_DISPATCH_SUCCEED;
                    message.obj = responseInfo.result;
                    handler.sendMessage(message);

                }

                @Override
                public void onFailure(HttpException e, String s) {
                    Log.e("0011", "获取所有派单信息失败" + s, e);
                    Message message = new Message();
                    message.what = MessageInfoUtil.GET_DISPATCH_FAIL;
                    message.obj = s;
                    handler.sendMessage(message);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    /**
     * 获取运单详情
     *
     * @param code
     * @param handler
     */
    public static void getGoodsDetails(String code, final Handler handler) {
        HttpUtils http = new HttpUtils();
        RequestParams params = new RequestParams();
        JSONObject obj = new JSONObject();
        try {
            obj.put("requestEntity", code);

            obj.put("type", HttpEnum.DRIVER_ORDER_GET_DETAIL.getCode());
            obj.put("operation", HttpEnum.DRIVER_ORDER_GET_DETAIL.getName());
            params.setBodyEntity(new StringEntity(obj.toString()));
            params.setContentType("application/json;charset=UTF-8");
            http.configTimeout(30 * 1000); // 配置超时时间：30秒
            String reqUrl = APP_SERVER_INTFACE_START
                    + APP_SERVER_INTFACE_NAME;
            http.send(HttpRequest.HttpMethod.POST, reqUrl, params, new RequestCallBack<String>() {

                @Override
                public void onSuccess(ResponseInfo<String> responseInfo) {
                    Log.e("0011", "获取运单信息" + responseInfo.result);
                    Message message = new Message();
                    message.what = MessageInfoUtil.GET_DISPATCH_SUCCEED;
                    message.obj = responseInfo.result;
                    handler.sendMessage(message);

                }

                @Override
                public void onFailure(HttpException e, String s) {
                    Log.e("0011", "获取运单信息" + s, e);
                    Message message = new Message();
                    message.what = MessageInfoUtil.GET_DISPATCH_FAIL;
                    message.obj = s;
                    handler.sendMessage(message);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void sign(String type, String code, String lat, String lon, String warehouseType, String sourceType, final Handler handler) {


        try {
            HttpUtils http = new HttpUtils();
            RequestParams params = new RequestParams();
            JSONObject obj = new JSONObject();
            JSONObject map = new JSONObject();
            map.put("lat", lat);
            map.put("lng", lon);
            map.put("appType", type);
            map.put("warehouseType", warehouseType);
            map.put("sourceType", sourceType);
            map.put("orderCode", code);
            obj.put("requestEntity", map);
            obj.put("type", HttpEnum.APPHANDLE.getCode());
            obj.put("operation", HttpEnum.APPHANDLE.getName());
            params.setBodyEntity(new StringEntity(obj.toString()));
            params.setContentType("application/json;charset=UTF-8");
            http.configTimeout(30 * 1000); // 配置超时时间：30秒
            String reqUrl = APP_SERVER_INTFACE_START
                    + APP_SERVER_INTFACE_NAME;
            http.send(HttpRequest.HttpMethod.POST, reqUrl, params, new RequestCallBack<String>() {

                @Override
                public void onSuccess(ResponseInfo<String> responseInfo) {
                    Log.e("0011", "签到信息" + responseInfo.result);
                    Message message = new Message();
                    message.what = MessageInfoUtil.SIGN_SUCCEED;
                    message.obj = responseInfo.result;
                    handler.sendMessage(message);

                }

                @Override
                public void onFailure(HttpException e, String s) {
                    Log.e("0011", "签到请求失败" + s, e);
                    Message message = new Message();
                    message.what = MessageInfoUtil.SIGN_FAIL;
                    message.obj = s;
                    handler.sendMessage(message);
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    public static void acceptOrder(String id, final Handler handler) {
        try {
            HttpUtils http = new HttpUtils();
            RequestParams params = new RequestParams();
            JSONObject obj = new JSONObject();
            obj.put("requestEntity", id);
            obj.put("type", HttpEnum.DRIVER_ORDER_ACCEPT.getCode());
            obj.put("operation", HttpEnum.DRIVER_ORDER_ACCEPT.getName());
            params.setBodyEntity(new StringEntity(obj.toString()));
            params.setContentType("application/json;charset=UTF-8");
            http.configTimeout(30 * 1000); // 配置超时时间：30秒
            String reqUrl = APP_SERVER_INTFACE_START + APP_SERVER_INTFACE_NAME;
            http.send(HttpRequest.HttpMethod.POST, reqUrl, params, new RequestCallBack<String>() {

                @Override
                public void onSuccess(ResponseInfo<String> responseInfo) {
                    Log.e("0011", "接单信息" + responseInfo.result);
                    Message message = new Message();
                    message.what = MessageInfoUtil.ACCEPT_ORDER_SUCCEED;
                    message.obj = responseInfo.result;
                    handler.sendMessage(message);

                }

                @Override
                public void onFailure(HttpException e, String s) {
                    Log.e("0011", "接单请求失败" + s, e);
                    Message message = new Message();
                    message.what = MessageInfoUtil.ACCEPT_ORDER_FAIL;
                    message.obj = s;
                    handler.sendMessage(message);
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static String getVersion(Context context) {
        try {
            PackageManager manager = context.getPackageManager();
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            String mVersionName = info.versionName;
            return mVersionName;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
